package com.yoogor.bsdiffpatch;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.yoogor.bsdiff.R;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        ImageView tv = (ImageView) findViewById(R.id.sample_text);
        ImageView tv1 = (ImageView) findViewById(R.id.sample_text1);
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
            InputStream inputStream = getAssets().open("1.jpg");
            InputStream inputStream1 = getAssets().open("3.jpg");
            File file1 = new File(getCacheDir(), "1.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream(file1);
            File file2 = new File(getCacheDir(), "2.jpg");
            File file3 = new File(getCacheDir(), "3.jpg");
            FileOutputStream fileOutputStream1 = new FileOutputStream(file3);
            file1.createNewFile();
            file2.createNewFile();
            file3.createNewFile();
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                bos.write(buf, 0, len);
                fileOutputStream.write(buf);
            }
            while ((len = inputStream1.read(buf)) > 0) {
                bos1.write(buf, 0, len);
                fileOutputStream1.write(buf);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            fileOutputStream1.flush();
            fileOutputStream1.close();
            byte[] bytes1 = bos.toByteArray();
            byte[] bytes2 = bos1.toByteArray();
            System.out.println(System.currentTimeMillis());
            patch(file1.getAbsolutePath(),file2.getAbsolutePath(),file3.getAbsolutePath());
            System.out.println(System.currentTimeMillis());
            byte[] bytes = bsdiffPatch(bytes1, bytes2);
            System.out.println(System.currentTimeMillis());
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            Bitmap bitmap1 = BitmapFactory.decodeFile(file2.getAbsolutePath());
            tv.setImageBitmap(bitmap);
            tv1.setImageBitmap(bitmap1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native int patch(String path1, String path2, String path3);

    public native int diff(String path1, String path2, String path3);

    public native byte[] bsdiffPatch(byte[] origin, byte[] patch);
}
